package org.hyn.gateway.executor;

import com.alibaba.fastjson.JSON;
import org.hyn.gateway.datasource.Connection;
import org.hyn.gateway.executor.result.SessionResult;
import org.hyn.gateway.mapping.HttpStatement;
import org.hyn.gateway.session.Configuration;
import org.hyn.gateway.type.SimpleTypeRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * @author franyhu
 * @description 执行器抽象基类
 * @date 2025/3/11
 */
public abstract class BaseExecutor implements Executor {

    private final Logger logger = LoggerFactory.getLogger(BaseExecutor.class);

    protected Configuration configuration;
    protected Connection connection;

    public BaseExecutor(Configuration configuration, Connection connection) {
        this.configuration = configuration;
        this.connection = connection;
    }

    @Override
    public SessionResult exec(HttpStatement httpStatement, Map<String, Object> params) throws Exception {
        // 参数处理。后续的参数校验也可以在这里封装
        String methodName = httpStatement.getMethodName();
        String parameterType = httpStatement.getParameterType();
        String[] parameterTypes = new String[]{parameterType};
        Object[] args = SimpleTypeRegistry.isSimpleType(parameterType) ? params.values().toArray() : new Object[]{params};
        logger.info("执行调用 method: {}#{}.{}({}) args: {}", httpStatement.getApplication(), httpStatement.getInterfaceName(), methodName, JSON.toJSONString(parameterTypes), JSON.toJSONString(args));

        // 抽象方法
        try {
            Object data = doExec(methodName, parameterTypes, args);
            return SessionResult.buildSuccess(data);
        } catch (Exception e) {
            return SessionResult.buildError(e.getMessage());
        }

    }

    protected abstract Object doExec(String methodName, String[] parameterTypes, Object[] args);
}
